package com.cuixichang.minimalism.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuixichang.minimalism.core.mybatis.cnstants.BaseFieldNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import com.cuixichang.minimalism.mapper.IFormPlugMapper;
import com.cuixichang.minimalism.transfer.dto.IFormPlugDTO;
import com.cuixichang.minimalism.transfer.factory.IFormPlugAssembler;
import com.cuixichang.minimalism.transfer.po.IFormPlug;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.function.Function;
import org.springframework.stereotype.Service;

/**
 * 表服务实现类
 *
 * @author cuixichang
 * @date 2024-12-18
 */
@Service
public class IFormPlugServiceImpl implements IFormPlugService {

    @Autowired
    private IFormPlugMapper iFormPlugMapper;

    @Override
    public IFormPlugDTO selectById(Long id){
        IFormPlug iFormPlug = iFormPlugMapper.selectById(id);
        return IFormPlugAssembler.INSTANCT.convertDto(iFormPlug);
    }

    @Override
    public IFormPlugDTO selectOne(IFormPlugDTO iFormPlugDTO){
        Assert.notNull(iFormPlugDTO,"参数为空,请检查");
        IFormPlug iFormPlug = IFormPlugAssembler.INSTANCT.convertPo(iFormPlugDTO);
        IFormPlug result = iFormPlugMapper.selectOne(new QueryWrapper<>(iFormPlug));
        return IFormPlugAssembler.INSTANCT.convertDto(result);
    }

    @Override
    public List<IFormPlugDTO> selectByIds(List<Long> ids) {
        Assert.notEmpty(ids,"参数为空,请检查");
        List<IFormPlug> results = iFormPlugMapper.selectBatchIds(ids);
        return results.stream().map(IFormPlugAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public List<IFormPlugDTO> selectList(IFormPlugDTO iFormPlugDTO){
        return this.selectList(iFormPlugDTO, (iFormPlug, queryWrapper) -> {});
    }

    @Override
    public List<IFormPlugDTO> selectList(IFormPlugDTO iFormPlugDTO, BiConsumer<IFormPlug, QueryWrapper<IFormPlug>> consumer){
        Assert.notNull(iFormPlugDTO,"参数为空,请检查");
        IFormPlug iFormPlug = IFormPlugAssembler.INSTANCT.convertPo(iFormPlugDTO);
        QueryWrapper<IFormPlug> queryWrapper = new QueryWrapper<>(iFormPlug);
        if(Objects.nonNull(consumer)){
            consumer.accept(iFormPlug, queryWrapper);
        }
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        List<IFormPlug> results = iFormPlugMapper.selectList(queryWrapper);
        return results.stream().map(IFormPlugAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public Page<IFormPlugDTO> selectPage(IFormPlugDTO iFormPlugDTO,long pageSize,long pageNo){
        return this.selectPage(iFormPlugDTO, pageSize, pageNo, (iFormPlug, queryWrapper) -> {});
    }

    @Override
    public Page<IFormPlugDTO> selectPage(IFormPlugDTO iFormPlugDTO,long pageSize,long pageNo, BiConsumer<IFormPlug, QueryWrapper<IFormPlug>> consumer){
        Assert.notNull(iFormPlugDTO,"参数为空,请检查");
        IFormPlug iFormPlug = IFormPlugAssembler.INSTANCT.convertPo(iFormPlugDTO);
        QueryWrapper<IFormPlug> queryWrapper = new QueryWrapper<>(iFormPlug);
        if(Objects.nonNull(consumer)) consumer.accept(iFormPlug, queryWrapper);
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        Page<IFormPlug> page = iFormPlugMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper);
        Page<IFormPlugDTO> result = Page.of(page.getCurrent(),page.getSize(),page.getTotal());
        result.setRecords(page.getRecords().stream()
                .map(IFormPlugAssembler.INSTANCT::convertDto).collect(Collectors.toList()));
        return result;
    }

    @Override
    public int deleteById(Long id){
        return iFormPlugMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Long> ids){
        return iFormPlugMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteByCondition(IFormPlugDTO iFormPlugDTO){
        return deleteByCondition(iFormPlugDTO, (iFormPlug, queryWrapper)-> {});
    }

    @Override
    public int deleteByCondition(IFormPlugDTO iFormPlugDTO, BiConsumer<IFormPlug, QueryWrapper<IFormPlug>> consumer){
        Assert.notNull(iFormPlugDTO,"参数为空,请检查");
        IFormPlug iFormPlug = IFormPlugAssembler.INSTANCT.convertPo(iFormPlugDTO);
        QueryWrapper<IFormPlug> queryWrapper = new QueryWrapper<>(iFormPlug);
        if(Objects.nonNull(consumer)) {
            consumer.accept(iFormPlug, queryWrapper);
        }
        return iFormPlugMapper.delete(queryWrapper);
    }

    @Override
    public IFormPlugDTO insert(IFormPlugDTO iFormPlugDTO){
        return insertHasOption(iFormPlugDTO, (IFormPlugDTO) -> null);
    }

    @Override
    public IFormPlugDTO insertHasOption(IFormPlugDTO iFormPlugDTO, Function<IFormPlugDTO,IFormPlugDTO> preOption){
        Assert.notNull(iFormPlugDTO,"参数为空,请检查");
        iFormPlugDTO.check();
        IFormPlug iFormPlug = IFormPlugAssembler.INSTANCT.convertPo(iFormPlugDTO);
        Long createLoginId = iFormPlug.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iFormPlug.setUpdateLoginId(createLoginId);
        LocalDateTime localDateTime = LocalDateTime.now();
        iFormPlug.setCreateTime(localDateTime);
        iFormPlug.setUpdateTime(localDateTime);
        Boolean isEnable = iFormPlug.getIsEnable();
        if(Objects.isNull(isEnable)) iFormPlug.setIsEnable(true);
        Integer sort = iFormPlug.getSort();
        if(Objects.isNull(sort)) iFormPlug.setSort(0);
        if(Objects.nonNull(preOption)){
            IFormPlugDTO apply = preOption.apply(iFormPlugDTO);
            if(Objects.nonNull(apply)) {
              return apply;
            }
        }
        int insert = iFormPlugMapper.insert(iFormPlug);
        Assert.isTrue(insert>0,"插入失败");
        return IFormPlugAssembler.INSTANCT.convertDto(iFormPlug);
    }

    @Override
    public int updateById(IFormPlugDTO iFormPlugDTO){
         return updateById(iFormPlugDTO, (iFormPlug, updateWrapper) -> {});
    }

    @Override
    public int updateById(IFormPlugDTO iFormPlugDTO, BiConsumer<IFormPlug, UpdateWrapper<IFormPlug>> consumer){
        Assert.notNull(iFormPlugDTO,"参数为空,请检查");
        Long id = iFormPlugDTO.getId();
        Assert.notNull(id,"主键为空,请检查");
        IFormPlug iFormPlug = IFormPlugAssembler.INSTANCT.convertPo(iFormPlugDTO);
        Long createLoginId = iFormPlug.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iFormPlug.setCreateLoginId(null);
        iFormPlug.setUpdateLoginId(createLoginId);
        iFormPlug.setUpdateTime(LocalDateTime.now());
        UpdateWrapper<IFormPlug> updateWrapper = new UpdateWrapper<IFormPlug>().eq(BaseFieldNames.ID, id);
        if(Objects.nonNull(consumer)) consumer.accept(iFormPlug,updateWrapper);
        return iFormPlugMapper.update(iFormPlug,updateWrapper);
    }
}
